var express = require('express');
var router = express.Router();

const sql = require("../sql/sql");
const address = require("../sql/address");
const tools = require('../tools/index')
const uuid = require('node-uuid')

/**
* @apiGroup 地址信息
* @api {post} /api/address/add 添加地址
* @apiVersion 0.0.0
* @apiDescription 添加地址
* @apiParam {string} userId 用户的唯一Id
* @apiParam {string} userName 用户的唯一账号
* @apiParam {string} tel 电话号
* @apiParam {string} addressDetail 详细地址
* @apiParam {string} Postal 邮政编码
* @apiSuccess {number} state 请求状态
* @apiSuccess {string} title 请求说明
* @apiSuccess {json} data 请求数据
* @apiSuccessExample {json} Success-Response:
res.send({
    state:200,
    title:"第一次添加地址成功"
})
res.send({
    state:200,
    title:"更新地址成功"
})
res.send({
    state:300,
    title:"登录信息已失效"
})
* @apiSampleRequest http://localhost:3000/api/address/add
*/


router.post("/add",function(req,res,next){
    const { userId,userName,tel,addressDetail,Postal } = req.body;
    tools.verify(req).then(()=>{
        sql.find(address,{userId}).then(data=>{
            if(data.length < 1){
                sql.insert(address,{
                    userId,
                    addData:[{
                        name:userName,
                        tel:tel,
                        postalCode:Postal,
                        address:addressDetail,
                        id:"add"+uuid.v1(),
                        isDefault:true
                    }]
                }).then(()=>{
                    res.send({
                        state:200,
                        title:"第一次添加地址成功"
                    })
                })
            }else{
                let arr = data[0].addData;
                arr.push({
                    name:userName,
                    tel:tel,
                    postalCode:Postal,
                    address:addressDetail,
                    id:"add"+uuid.v1(),
                    isDefault:false
                })
                sql.update(address,{userId},{addData:arr}).then(()=>{
                    res.send({
                        state:200,
                        title:"更新地址成功"
                    })
                })
            }
        })
    }).catch(err=>{
        res.send({
            state:300,
            title:"登录信息已失效"
        })
    })
})

/**
* @apiGroup 地址信息
* @api {get} /api/address 获取地址信息
* @apiVersion 0.0.0
* @apiDescription 获取地址信息
* @apiParam {string} userId 用户的唯一Id
* @apiSuccess {number} state 请求状态
* @apiSuccess {string} title 请求说明
* @apiSuccess {json} data 请求数据
* @apiSuccessExample {json} Success-Response:
res.send({
    state:200,
    title:"请求地址信息成功"
    data:data[0]
})
* @apiSampleRequest http://localhost:3000/api/address
*/

router.get("/",function(req,res,next){
    const { userId } = req.query;
    tools.verify(req).then(()=>{
        sql.find(address,{userId},{_id:0,__v:0}).then(data=>{
            res.send({
                state:200,
                title:"请求地址信息成功",
                data:data[0]
            })
        })
    }).catch(err=>{
        res.send({
            state:300,
            title:"登录信息已失效"
        })
    })
})


module.exports = router;